<?php
//创建唯一UUID
function create_uuid($num = 32)
{
	$chars = md5(uniqid(mt_rand(), true));  
	$uuid = substr ( $chars, 0, $num );
	return $uuid;
}
//逗号分割
function explodeId($string)
{
	$string = trim($string, ',');
	if(strstr($string, ','))
	{
		return explode(',', $string);
	}else{
		return [$string];
	}
}
//select选择options 重组
function selectOptionsField($datas, $field = '')
{
	$ret = [];
	if(!$datas)return $ret;
	foreach($datas as $row)
	{
		if($field == '')
		{
			$ret[$row->id] = $row;
		}else{
			$ret[$row->id] = $row->$field;
		}
		
	}
	return $ret;
}
//获取新闻的链接
function news_url($created_at, $id, $domain = false)
{
	$url = '/html/'.$created_at->format('Y/m').'/'.$id.env('BUILD_EXT');
	if($domain)$url = env('PREVIEW_DOMAIN').$url;
	return $url;
}
//展览url
function zl_url($created_at, $id, $domain = false)
{
	$url = '/html/exh/detail/'.$created_at->format('Y/m/d').'/'.$id.'.htm';
	if($domain)$url = env('ZL_DOMAIN').$url;
	return $url;
}
//展览图片url
function zl_photo_url($created_at, $id, $domain = false)
{
	$url = '/html/exh/photo/'.$created_at->format('Y/m/d').'/'.$id.'.htm';
	if($domain)$url = env('ZL_DOMAIN').$url;
	return $url;
}

function getCurrentAction()
{
    $action = \Route::current()->getActionName();
    list($class, $method) = explode('@', $action);

    return ['controller' => $class, 'method' => $method];
}
//替换生成静态分页的时候链接
function fenyeUrl($url, $domain)
{
	return str_replace('work.manage.zhuoke.cn', $domain, $url);
}
//提供全局公共变量
function replaceContent($content)
{
	$global_var = [
		'__STATIC__' => env('STATIC_DOMAIN'),
		'__SKIN__' => env('STATIC_DOMAIN').'/skin',
		'__MAIN__' => env('MAIN_DOMAIN'),
	];
	foreach($global_var as $key=>$row)
	{
		$content = str_replace($key, $row, $content);
	}
	return $content;
}
//缩略图地址
function thumbUrl($url)
{
	return env('STATIC_DOMAIN').'/'.env('UPLOAD_TITLEPIC').'/'.$url;
}

use Illuminate\Support\Facades\Storage;
function thumbOssTitlepicUrl($url)
{
	if(strstr($url, 'http'))return $url;
	if(strstr($url, '/upload'))return env('STATIC_DOMAIN').$url;
	$disk = Storage::disk('admin');
	return $disk->url($url);
}
//模板输出多于变量
function getFieldValue($arr, $field)
{
	if(!is_array($arr))return '变量不是数组';
	if(array_key_exists($field, $arr))
	{
		//如果是图片，则加上url
		if(strstr($field, 'thumb')){
			return thumbTitlepicUrl($arr[$field]);
		}
		return $arr[$field];
	}else{
		return '';
	}
}
//图集显示缩略图
function atlasThumb($val)
{
	return $val.'?x-oss-process=style/140';
	// return str_replace_last('.', '_200.', $val);
	// $end = strrpos($val, '.');
	
	// if($end)
	// {
	// 	return substr_replace($val, '_200', $end-1);
	// }
	// return $val;
}

function jumpUrl($url)
{
	header('Location: '.$url);
}

//获取栏目的链接
function menuUrl($class_id)
{
	if(is_numeric($class_id))
		$info = \App\Models\News\NewsClass::find($class_id);
	else
		$info = $class_id;

	if(!$info || empty($info->alias))return '';
	return '/'.$info->alias.'/index.html';
}
//当前位置
function nowNav($row)
{
	if(!$row)return '';
	if(isset($row->parent) && !empty($row->parent))
	{
		if(isset($row->parent->parent) && !empty($row->parent->parent))
		{
			return ' &gt; <a href="'.menuUrl($row->parent->parent).'">'.$row->parent->parent->name.'</a> &gt; <a href="'.menuUrl($row->parent).'">'.$row->parent->name.'</a> &gt; <a href="'.menuUrl($row).'">'.$row->name.'</a>';
		}
		return ' &gt; <a href="'.menuUrl($row->parent).'">'.$row->parent->name.'</a> &gt; <a href="'.menuUrl($row).'">'.$row->name.'</a>';
	}else{
		return ' &gt; <a href="'.menuUrl($row).'">'.$row->name.'</a>';
	}
}